package cc.comac.mom.webservice.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cc.comac.mom.bean.QualitySapPurchaseOrder;
import cc.comac.mom.bean.QualitySapPurchaseOrderBean;
import cc.comac.mom.datahandlers.entity.QualityQarCheckInvoiceDataHandler;
import cc.comac.mom.datahandlers.entity.QualitySapPurchaseOrderDataHandler;
import cc.comac.mom.entity.QualitySapPurchaseOrderEntity;
import cc.comac.mom.util.ResultBean;
import cc.comac.mom.webservice.QualitySapPurchaseOrderWebService;

@WebService(serviceName = "PurchaseOrder",targetNamespace = "http://webservice.mom.comac.cc/",endpointInterface = "cc.comac.mom.webservice.QualitySapPurchaseOrderWebService")
@Component
@Transactional
public class QualitySapPurchaseOrderWebServiceImpl implements QualitySapPurchaseOrderWebService{

	@Autowired
	private QualitySapPurchaseOrderDataHandler qualitySapPurchaseOrderDataHandler;
	
	@Autowired
	private QualityQarCheckInvoiceDataHandler qualityQarCheckInvoiceDataHandler;
	@Override
	public String getUserByName() {
		// TODO Auto-generated method stub
		return null;
	}
	// 生成qar
	@Override
	public String insertBatch(List<QualitySapPurchaseOrderBean> list) {
		ResultBean resultBean = new ResultBean(true);
		resultBean.setMessage("接收成功！");
		if(list == null || list.size()<1) {
			resultBean.setErrorMessge("数据为空");
			return resultBean.toString();
		}
		ArrayList<QualitySapPurchaseOrder> list2 = new ArrayList<QualitySapPurchaseOrder>();
		try {
			for(QualitySapPurchaseOrderBean item:list) {
				QualitySapPurchaseOrder qualitySapPurchaseOrder = new QualitySapPurchaseOrder();
				QualitySapPurchaseOrderEntity qualitySapPurchaseOrderEntity = new QualitySapPurchaseOrderEntity();
				qualitySapPurchaseOrderEntity.setBanCi(item.getBanCi());
				qualitySapPurchaseOrderEntity.setBatchNumber(item.getBatchNumber());
				qualitySapPurchaseOrderEntity.setBom(item.getBom());
				qualitySapPurchaseOrderEntity.setCaiLiaoXingShi(item.getCaiLiaoXingShi());
				qualitySapPurchaseOrderEntity.setContractNo(item.getContractNo());
				qualitySapPurchaseOrderEntity.setDengXiaoGuiFan(item.getDengXiaoGuiFan());
				qualitySapPurchaseOrderEntity.setDengXiaoJianHao(item.getDengXiaoJianHao());
				qualitySapPurchaseOrderEntity.setVbeln(item.getVbeln());
				qualitySapPurchaseOrderEntity.setGuiFan(item.getGuiFan());
				qualitySapPurchaseOrderEntity.setGuiGe(item.getGuiGe());
				qualitySapPurchaseOrderEntity.setJi(item.getJi());
				qualitySapPurchaseOrderEntity.setLei(item.getLei());
				qualitySapPurchaseOrderEntity.setMaterial(item.getMaterial());
				qualitySapPurchaseOrderEntity.setMaterialDescription(item.getMaterialDescription());
				qualitySapPurchaseOrderEntity.setMaterialDocument(item.getMaterialDocument());
				qualitySapPurchaseOrderEntity.setMaterialDocumentItem(item.getMaterialDocumentItem());
				qualitySapPurchaseOrderEntity.setMaterialDocumentYear(item.getMaterialDocumentYear());
				qualitySapPurchaseOrderEntity.setMaterialGroup(item.getMaterialGroup());
				qualitySapPurchaseOrderEntity.setMaterialGroupDescription(item.getMaterialGroupDescription());
				qualitySapPurchaseOrderEntity.setMaterialType(item.getMaterialType());
				qualitySapPurchaseOrderEntity.setMaterialTypeDescription(item.getMaterialTypeDescription());
				qualitySapPurchaseOrderEntity.setPaiHao(item.getPaiHao());
				qualitySapPurchaseOrderEntity.setPlant(item.getPlant());
				qualitySapPurchaseOrderEntity.setPostingDate(item.getPostingDate());
				qualitySapPurchaseOrderEntity.setPotypeDescription(item.getPotypeDescription());
				qualitySapPurchaseOrderEntity.setPurchaseOrder(item.getPurchaseOrder());
				qualitySapPurchaseOrderEntity.setPurchaseOrderDate(item.getPurchaseOrderDate());
				qualitySapPurchaseOrderEntity.setPurchaseOrderQuantity(new BigDecimal(item.getPurchaseOrderQuantity()));
				qualitySapPurchaseOrderEntity.setPurchaseOrderItem(item.getPurchaseOrderItem());
				qualitySapPurchaseOrderEntity.setPurchaseOrderType(item.getPurchaseOrderType());
				qualitySapPurchaseOrderEntity.setPurchaseOrderUom(item.getPurchaseOrderUom());
				qualitySapPurchaseOrderEntity.setPurchaseOrganization(item.getPurchaseOrganization());
				qualitySapPurchaseOrderEntity.setPurchaseOrganizationDesc(item.getPurchaseOrganizationDesc());
				qualitySapPurchaseOrderEntity.setQuantity(new BigDecimal(item.getQuantity()));
				qualitySapPurchaseOrderEntity.setRuanJianBanBen(item.getRuanJianBanBen());
				qualitySapPurchaseOrderEntity.setRuanJianJianHao(item.getRuanJianJianHao());
				qualitySapPurchaseOrderEntity.setSe(item.getSe());
				qualitySapPurchaseOrderEntity.setSerialNumber(item.getSerialNumber());
				qualitySapPurchaseOrderEntity.setStockQuantity(new BigDecimal(item.getStockQuantity()));
				qualitySapPurchaseOrderEntity.setStockUnit(item.getStockUnit());
				qualitySapPurchaseOrderEntity.setStorageLocation(item.getStorageLocation());
				qualitySapPurchaseOrderEntity.setUsername(item.getUsername());
				qualitySapPurchaseOrderEntity.setVendor(item.getVendor());
				qualitySapPurchaseOrderEntity.setVendorBatchNumber(item.getVendorBatchNumber());
				qualitySapPurchaseOrderEntity.setVendorName(item.getVendorName());
				qualitySapPurchaseOrderEntity.setWbsdescription(item.getWbsdescription());
				qualitySapPurchaseOrderEntity.setWbsnumber(item.getWbsnumber());
				qualitySapPurchaseOrderEntity.setXing(item.getXing());
				qualitySapPurchaseOrderEntity.setZhuangTaiXingShi(item.getZhuangTaiXingShi());
				qualitySapPurchaseOrderEntity.setZpmCpmSpm(item.getZpmCpmSpm());
				qualitySapPurchaseOrder.setInnerData(qualitySapPurchaseOrderEntity);
				list2.add(qualitySapPurchaseOrder);
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
			resultBean.setSuccess(false);
			resultBean.setMessage(e.toString());
			return resultBean.toString();
		}
		// 自动生成qar检验表单
		if (list2.size() > 0) {
				qualityQarCheckInvoiceDataHandler.createQar(list2);
		}
		
		return resultBean.toString();
	}

}
